from datetime import datetime, timedelta, date
from datetime import time
import time as tm


class DailyDateIterator(object):
    """
    日级别日期迭代器
    """

    def __init__(self, start, end):
        self.date = start
        self.end = end

    def __iter__(self):
        return self

    def __next__(self):
        if self.date <= self.end:
            val = self.date
            self.date = val + timedelta(days=1)
            return val
        else:
            raise StopIteration


def utc_now():
    return datetime


def parse_start_end_time(start, end, hour_interval=1):
    if start is None or end is None:
        now = datetime.now()
        end = datetime(now.year, now.month, now.day, hour=now.hour, minute=now.minute, second=now.second)
        start = end - timedelta(hours=hour_interval)
        return start, end

    return parse_date_time(start), parse_date_time(end)


def parse_date_time(dt_str, fmt="%Y-%m-%d %H:%M:%S") -> datetime:
    """
    解析时间
    :return:
    """
    if dt_str is None or '' == dt_str: return None

    time = datetime.strptime(dt_str, fmt)
    return time


def date_time_to_str(dt: datetime, fmt="%Y-%m-%d %H:%M:%S"):
    return dt.strftime(fmt)


def get_days_between(end: datetime, start: datetime) -> int:
    secokds = (get_start_of_day(end) - get_start_of_day(start)).total_seconds()

    return int(secokds / (24 * 60 * 60))


def get_start_of_day(dt: datetime) -> datetime:
    return datetime(dt.year, dt.month, dt.day)


def get_end_of_day(dt: datetime) -> datetime:
    return datetime.combine(dt, time.max)


def get_start_of_hour(dt: datetime) -> datetime:
    return datetime(dt.year, dt.month, dt.day, dt.hour, 0, 0)


def get_start_of_time_level(dt: datetime, level):
    if level == 'hour':
        return get_start_of_hour(dt)

    if level == 'day':
        return get_start_of_day(dt)

    return None


def get_next_of_hour(dt: datetime) -> datetime:
    return datetime(dt.year, dt.month, dt.day, dt.hour, 0, 0) + timedelta(hours=1)


def day_incr(dt: datetime, days):
    return dt + timedelta(days=days)


def dt_to_str(dt: datetime, fmt="%Y%m%d") -> str:
    return dt.strftime(fmt)


def get_friday_of_week():
    now = datetime.now()
    friday = datetime.now() + timedelta(days=4 - now.weekday())
    return friday


def to_timestamp(dt: datetime):
    """
    :param dt:
    :return:
    """
    if dt is None: return dt

    return int(tm.mktime(dt.timetuple()) * 1000.0 + dt.microsecond / 1000.0)


def from_timestamp(timestamp):
    timeStamp = float(timestamp) / 1000
    ret_datetime = datetime.fromtimestamp(timeStamp)
    return ret_datetime


def is_weekend(t) -> bool:
    return t.weekday() >= 5


def is_current_hour(time: datetime):
    now = datetime.now()
    return time.year == now.year \
           and time.month == now.month \
           and time.day == now.day \
           and time.hour == now.hour


# def get_work_day(now, offset):
#     i = 1
#     while i <= offset:
#         t = now - timedelta(days=i)
#         if is_workday(t) is False or is_weekend(t):
#             offset = offset + 1
#         i = i + 1
#
#     return now - timedelta(days=offset), now


# def if_workday(dt):
#         '''
#         判断是否为工作日
#         '''
#         Y = dt.year
#         M = dt.month
#         D = dt.day
#         april_last = date(Y, M, D)
#         return is_workday(april_last)

def is_same_hour(dt1: datetime, dt2: datetime) -> bool:
    if dt1 is None or dt2 is None: return False
    return dt1.year == dt2.year \
           and dt1.day == dt2.day \
           and dt1.month == dt2.month \
           and dt1.hour == dt2.hour


if __name__ == "__main__":
    pass
    # d = parse_date_time("20210823212121", fmt="%Y%m%d%H%M%S")
    # d2 = parse_date_time("20210829121212", fmt="%Y%m%d%H%M%S")
    print(dt_to_str(datetime.now(), "%Y-%m-%dT%H:%M:%SZ"))
    # print(get_friday_of_week())
    # now = datetime.now() - timedelta(days=10)
    # print(now.weekday())
    # print(is_workday(datetime(2021,10,7)))

    # a = set(DailyDateIterator(datetime(2023,1,1),datetime.now()))
    # print(a)
    print(is_current_hour(get_start_of_hour(datetime.now())))
